package com.spx.kernel.types;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.persistence.Version;

@MappedSuperclass
public abstract class AbstractModelType implements ModelType
{
	
	@Id
	String id;

	@Version
	int version;
	
	public final int getVersion()
	{
		return version;
	}
	
	public final void setVersion(int version)
	{
		this.version = version;
	}
	

	@Column(unique = true, nullable = false)
	String name;
	public AbstractModelType()
	{
		id = UUID.randomUUID().toString();
	}

	@Override
	public String getId()
	{
		return id;
	}

	@Override
	public String getName()
	{
		return name;
	}

	@Override
	public void setName(final String name)
	{
		final String oldName = this.name;
		this.name = name;
		if (oldName == null && name != null || (name != null && name.equals(oldName) == false))
		{
			changeSupport.firePropertyChange("name", oldName, name);
		}
	}

	// --------------------------------Bean helpers
	// --------------------------------

	@Transient
	protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

	public void addPropertyChangeListener(final PropertyChangeListener listener)
	{
		changeSupport.addPropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(final PropertyChangeListener listener)
	{
		changeSupport.removePropertyChangeListener(listener);
	}

}
